package com.hyy.springbootblog.controller;

import com.hyy.springbootblog.common.pojo.request.AddBlogParam;
import com.hyy.springbootblog.common.pojo.request.UpBlogParam;
import com.hyy.springbootblog.common.pojo.response.BlogInfoResponse;
import com.hyy.springbootblog.service.BlogService;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/blog")
public class BlogController {

    //    @Autowired
//    private BlogService blogServiceImpl;//autowired可以但是，变量名必须和实现类一致
    @Resource(name = "blogServiceImpl")//name要和实现类一致（如果数据库有修改，更改这个name即可，解耦合）
    private BlogService blogService;

    @GetMapping("/getList")
    public List<BlogInfoResponse> getList() {
        log.info("获取博客列表...");
        return blogService.getList();
    }

    @GetMapping("/getBlogDetail")//NotNull 这个注解自动校验参数不为空（javaee jakta中的）
    public BlogInfoResponse getBlogDetail(@NotNull Integer blogId) {
        log.info("获取图书详情,blogId={}", blogId);
        return blogService.getBlogDetail(blogId);
    }

    @PostMapping("/add")//json传递的是
    public Boolean addBlog(@Validated @RequestBody AddBlogParam addBlogParam) {
        log.info("添加博客，标题：{}", addBlogParam.getTitle());
        return blogService.addBlog(addBlogParam);
    }

    @PostMapping("/updateBlog")
    public Boolean updateBlog(@Validated @RequestBody UpBlogParam upBlogParam) {
        log.info("更新博客,博客Id={}", upBlogParam.getId());
        return blogService.updateBlog(upBlogParam);
    }

    @PostMapping("/deleteBlog")
    public Boolean deleteBlog(@NotNull Integer blogId) {
        log.info("删除博客,博客Id={}", blogId);
        return blogService.deleteBlog(blogId);
    }
}
